/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package parser.nodes;

import codegen.CodeGen;
import java.util.ArrayList;
import java.util.List;
import parser.Pair;
import scanner.Token;

// Term = Factor {('*' | '/') Factor}
public class TermNode extends Node {
    private FactorNode factor;
    private List<Pair<Token,FactorNode>> factorList = new ArrayList<>();
    
    public TermNode(FactorNode factor) {
        this.factor = factor;
    }            
    
    public void addFactor(Token operator, FactorNode factor) {
        factorList.add(new Pair<>(operator, factor));
    }
	
	public void removeCont() {
		if (factorList.isEmpty())
			factor.removeCont();
	}
    
    @Override
    public void print() {
        p("Term:");
        indent();
        factor.print();
        for(Pair<Token,FactorNode> pair : factorList) {
            p(pair.getFirst());
            pair.getSecond().print();
        }
        unindent();
    }

    @Override
    public void abstractPrint() {
        factor.abstractPrint();
        for(Pair<Token,FactorNode> pair : factorList) {
            p(pair.getFirst());
            indent();
            pair.getSecond().abstractPrint();
            unindent();
        }
    }

    @Override
    public void compile(CodeGen c) throws CompileError {
        factor.compile(c);
        
        for(Pair<Token,FactorNode> pair : factorList) {
            pair.getSecond().compile(c);
            if (pair.getFirst().is("*"))
                c.outstr("MUL");
            else
                c.outstr("DIV");
        }
    }
    
    
}
